package com.javaee.xiangyang.controller.mall;

import com.javaee.xiangyang.dto.GoodsDetails;
import com.javaee.xiangyang.entity.Classify;
import com.javaee.xiangyang.entity.Goods;
import com.javaee.xiangyang.service.GoodsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/MallGoods")
public class MallCommodityController {

    @Resource
    GoodsService goodsService;

    /**
     * 前往主页
     * 传递随机八个数据
     */
    @RequestMapping("/index")
    public ModelAndView index(ModelAndView modelAndView) {
        //获取全部分类信息
        List<Classify> classifies = goodsService.findAllClassify();
        //全部信息
        List<Goods> allGoods = goodsService.findGoodsOrderBy();
        List<Goods> allGoods1 = goodsService.findGoodsOrderBy();
        //打乱顺序
        Collections.shuffle(allGoods);
        modelAndView.setViewName("/mall/index");
        //随机显示三个类别
        modelAndView.addObject("classify", classifies.subList(0, 3));
        //购买排序5条信息
        modelAndView.addObject("goodsOrderBy", allGoods1.subList(0, 5));
        //每日推荐8条信息
        modelAndView.addObject("goods", allGoods.subList(0, 8));
        return modelAndView;
    }

    /**
     * 前往对应商品具体界面
     */
    @RequestMapping("/GoodsDetail")
    public ModelAndView detailPage(ModelAndView modelAndView, Goods goods) {
        //获取对应信息
        GoodsDetails goodsDetails = goodsService.findGoodsDetails(goods);
        //获取所有分类信息
        List<Classify> allClassify = goodsService.findAllClassify();
        modelAndView.addObject("allClassify", allClassify);
        modelAndView.addObject("goodsDetails", goodsDetails);
        modelAndView.setViewName("/mall/detail");
        return modelAndView;
    }


    /**
     * 前往分类界面
     */
    @RequestMapping("/ToClassify")
    public ModelAndView toClassify(ModelAndView modelAndView, Classify classify) {
        //获取所有分类信息
        List<Classify> allClassify = goodsService.findAllClassify();
        if (classify.getClassifyId() != 0) {
            List<Goods> allGoods = goodsService.findAllGoodsByClassifyId(classify);
            modelAndView.addObject("allGoods", allGoods);
        }
        modelAndView.addObject("allClassify", allClassify);
        modelAndView.setViewName("/mall/category");
        return modelAndView;
    }

    /**
     * 模糊查询
     */
    @RequestMapping("/SelectBySomething")
    public ModelAndView selectBySomething(ModelAndView modelAndView, String string) {
        //获取所有分类信息
        List<Classify> allClassify = goodsService.findAllClassify();
        //封装类型
        Classify classify = new Classify();
        classify.setClassifyId(0);
        //模糊查询
        List<GoodsDetails> goodsBySomething = goodsService.findGoodsBySomething(string, classify);
        //解开封装还原为Goods
        List<Goods> goodsList = new ArrayList<>();
        for (GoodsDetails goodsDetails : goodsBySomething) {
            goodsList.add(goodsDetails.getGoods());
        }
        modelAndView.addObject("allGoods", goodsList);
        modelAndView.addObject("allClassify", allClassify);
        modelAndView.setViewName("/mall/category");
        return modelAndView;
    }
}
